package com.example.fuckyou;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MyDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "MyDatabase.db";
    private static final int DATABASE_VERSION = 1;
    private Context context;
    // 創建 Lesson 表格的 SQL 語句
    private static final String CREATE_TABLE_LESSONS =
            "CREATE TABLE lessons (" +
                    "name TEXT, " +
                    "teacher TEXT, " +
                    "classroom TEXT, " +
                    "fromClass INTEGER, " +
                    "endClass INTEGER, " +
                    "day INTEGER, " +
                    "date TEXT);"; // 儲存日期，使用 ISO 格式 (yyyy-MM-dd)
    private static final String CREATE_TABLE_WEEKNUM=
            "CREATE TABLE weeknum("+
                    "weeknum INTEGER,"+
                    "date TEXT);";
    public MyDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_LESSONS); // 執行創建表格 SQL
        db.execSQL(CREATE_TABLE_WEEKNUM);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS lessons");
        db.execSQL("DROP TABLE IF EXISTS weeknum");
        onCreate(db);
    }
    public void insertweek(int weeknum,String date)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("weeknum",weeknum);
        values.put("date",date);
        db.insert("weeknum",null,values);
    }
    @SuppressLint("Range")
    public int getweeknum(String date)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        String[] args=new String[]{date};
        Cursor cursor=db.query("weeknum",null,"date=?",args,null,null,null);
        int ans=0;
        while(cursor.moveToNext())
        {
            ans=cursor.getInt(cursor.getColumnIndex("weeknum"));
        }
        return ans;
    }
    @SuppressLint("Range")
    public Date getweekdate(int i) throws ParseException {
        SQLiteDatabase db=this.getWritableDatabase();
        String[] args=new String[]{String.valueOf(i)};
        Log.d("ddok?",String.valueOf(i));
        String ans="";
        Cursor cursor=db.query("weeknum",null,"weeknum=?",args,null,null,null);
        {
            while(cursor.moveToNext())
            {
                ans=cursor.getString(cursor.getColumnIndex("date"));
            }
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-M-d");
        Date r=sdf.parse(ans);
        return r;
    }
    public void insertLesson(Lesson lesson) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
        values.put("name", lesson.name);
        values.put("teacher", lesson.teacher);
        values.put("classroom", lesson.classroom);
        values.put("fromClass", lesson.fromClass);
        values.put("endClass", lesson.endClass);
        values.put("day", lesson.day);
        values.put("date", dateFormat.format(lesson.date));
        db.insert("lessons", null, values);
    }
    public void deleteLesson()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete("lessons",null,null);
    }
    @SuppressLint("Range")
    public List<Lesson> alllesson() throws ParseException {
        List<Lesson> data=new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor=db.query("lessons", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            Lesson temp=new Lesson();
            temp.name = cursor.getString(cursor.getColumnIndex("name"));
            temp.teacher = cursor.getString(cursor.getColumnIndex("teacher"));
            temp.classroom = cursor.getString(cursor.getColumnIndex("classroom"));
            temp.fromClass = cursor.getInt(cursor.getColumnIndex("fromClass"));
            temp.endClass = cursor.getInt(cursor.getColumnIndex("endClass"));
            temp.day = cursor.getInt(cursor.getColumnIndex("day"));
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-M-d");
            temp.date= dateFormat.parse(cursor.getString(cursor.getColumnIndex("date")));
            data.add(temp);
        }
        cursor.close();
        return data;
    }
    @SuppressLint("Range")
    public List<Lesson> lessoninweek(Date date) throws ParseException
    {
        List<Lesson> data=new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        List<Date> tt=new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-M-d");
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        tt.add(date);
        for (int i=0;i<6;i++)
        {
            calendar.add(Calendar.DATE,1);
            Date temp=calendar.getTime();
            tt.add(temp);
        }
        String []args=new String[]{sdf.format(tt.get(0)), sdf.format(tt.get(1)),sdf.format(tt.get(2)), sdf.format(tt.get(3)),sdf.format(tt.get(4)), sdf.format(tt.get(5)),sdf.format(tt.get(6))};
        Cursor cursor = db.query("lessons", null, "date = ? OR date = ? OR date = ? OR date = ? OR date = ? OR date = ? OR date = ?",args , null, null, null);
        while (cursor.moveToNext()) {
            Lesson temp=new Lesson();
            temp.name = cursor.getString(cursor.getColumnIndex("name"));
            temp.teacher = cursor.getString(cursor.getColumnIndex("teacher"));
            temp.classroom = cursor.getString(cursor.getColumnIndex("classroom"));
            temp.fromClass = cursor.getInt(cursor.getColumnIndex("fromClass"));
            temp.endClass = cursor.getInt(cursor.getColumnIndex("endClass"));
            temp.day = cursor.getInt(cursor.getColumnIndex("day"));
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-M-d");
            temp.date= dateFormat.parse(cursor.getString(cursor.getColumnIndex("date")));
            data.add(temp);
            Log.d("hahaha","scuess");
        }
        return data;
    }
}
